package com.loop.unclelend.system.device.services

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.BatteryManager
import android.os.Build
import com.google.gson.Gson
import com.loop.unclelend.system.device.bean.DVBatteryBean

class DVBatteryService {
    private var _batteryInfoListener: OnBatteryInfoListener? = null
    private var _batteryFilter: IntentFilter? = null
    private var _context: Context? = null
    private var _curLevel: String = "0"
    private var _isCharging: String = "NO"
    private var _isUsbCharging: String = "NO"
    private var _isAcCharging: String = "NO"
    private val _batteryReceiver: BroadcastReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            try {
                val status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1)
                if (BatteryManager.BATTERY_STATUS_CHARGING == status ||
                    BatteryManager.BATTERY_STATUS_FULL == status
                ) {
                    _isCharging = "YES"
                }

                val chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)
                val isUsbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB
                val isAcCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC
                if (isUsbCharge) {
                    _isUsbCharging = "YES"
                    _isAcCharging = "NO"
                }
                if (isAcCharge) {
                    _isUsbCharging = "NO"
                    _isAcCharging = "YES"
                }

                val level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0)
                _curLevel = level.toString()

                if (_batteryInfoListener != null) {
                    val bean = DVBatteryBean(
                        isCharging = _isCharging,
                        batteryPct = _curLevel,
                        isAcCharge = _isAcCharging,
                        isUsbCharge = _isUsbCharging
                    )
                    val zipString = Gson().toJson(bean).trim().gzipString()
                    _batteryInfoListener?.onFetchSuccess(zipString)
                }
                clean()
            } catch (e: Exception) {
                e.printStackTrace()
                clean()
            }
        }
    }

    fun get(context: Context, batteryInfoListener: OnBatteryInfoListener) {
        _context = context
        _batteryInfoListener = batteryInfoListener

        try {
            _batteryFilter = IntentFilter(Intent.ACTION_BATTERY_CHANGED)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                context.registerReceiver(_batteryReceiver, _batteryFilter, Context.RECEIVER_EXPORTED)
            } else {
                context.registerReceiver(_batteryReceiver, _batteryFilter)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            context.unregisterReceiver(_batteryReceiver)
            val bean = DVBatteryBean(
                isCharging = _isCharging,
                batteryPct = _curLevel,
                isAcCharge = _isAcCharging,
                isUsbCharge = _isUsbCharging
            )
            val zipString = Gson().toJson(bean).trim().gzipString()
            batteryInfoListener.onFetchSuccess(zipString)
        }
    }

    fun unRegister(context: Context) {
        try {
            context.unregisterReceiver(_batteryReceiver)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    fun clean() {
        _context?.unregisterReceiver(_batteryReceiver)
        _batteryFilter = null
        _batteryInfoListener = null
    }

    interface OnBatteryInfoListener {
        fun onFetchSuccess(zipString: String)
    }
}
